<?php
namespace Admin\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function d_login()
    {
        if (IS_POST) {
            $user = I('post.user');
            $password = I('post.password');
            $manager = M('manager');
            $result = $manager->query("select * from manager where mn_user='$user' and mn_password='$password'");
            $name = $result[0]['mn_user'];
            if ($result) {
                session_start();
                $_SESSION['manager'] = "$name";
                $this->redirect('manage', '登录成功', 0);
            } else {
                echo "密码错误";
            }
        }
        $this->display();
    }

    //机构认证确认
    public function check_type()
    {
        $user = M('user');
        $team_list = $user->where("u_type=0 and real_status=0")->select();
        $this->assign('team_list', $team_list);
        $this->display();
    }

    //机构详情
    public function type_infor($id)
    {
        $id = (int)(I('get.id'));
        $one_list = M('user')->where("u_id='$id'")->select();
        $this->assign('one_list', $one_list);
        $this->display();
    }

    //确认机构
    public function type_confirm($id)
    {
        if (empty($_SESSION['manager'])) {
            $this->redirect('d_login', '未登录', 2);
        } else {
            $id = I('get.id');
            $data['real_status'] = 1;
            $rel = M('user')->where("u_id='$id'")->save($data);
            if ($rel) {
                $this->success('成功', U('index/check_type'), 2);
            } else {
                $this->error('失败', U('index/check_type'), 2);
            }
        }
    }

    //检录详情
    public function check_ch($id)
    {
        $g_id = (int)(I('get.id'));
        $join_game = M('jon_game')
            ->join("user on user.u_id=jon_game.u_id")
            ->join("LEFT JOIN team on team.admin_uid=user.u_id")
            ->where("jon_game.g_id='$g_id' and user.u_type!='0'")
            ->select();
        $wb_ids = M('jon_game')->where("g_id='$g_id'")->select('wb_list');
        $data['u_id'] = array('like', '%' . $wb_ids . '%');
        $wb_s = M('user')->join("LEFT JOIN team on team.admin_uid=user.u_id")->where($data)->select();
        $this->assign('wb_s', $wb_s);
        $this->assign('join_g_list', $join_game);
        $this->display();
    }

    //主页
    public function index()
    {
        $user = M('user');
        $user_count = $user->where("u_type=1")->count();
        $team_count = $user->where("u_type=0")->count();
        $this->assign('user_count', $user_count);
        $this->assign('team_count', $team_count);
        $game = M('game');
        $game_list = $game->count();
        $this->assign('game_list', $game_list);
        $this->display();
    }

    public function manage()
    {
        if (empty($_SESSION['manager'])) {
            $this->redirect('d_login', '未登录', 2);
        } else {
            $name = $_SESSION['manager'];
            $manager = M('manager');
            $auth = M('mn_auth');
            $auth_id = $manager->query("select auth_id from manager where mn_user='$name'");
            $auth_id = $auth_id[0]['auth_id'];
            $this->assign('auth_id', $auth_id);
            $this->display();
        }
    }

    //明细页面
    public function applied($id)
    {
        if (!($_SESSION['manager'])) {
        } else {
            $u_id = (int)($_GET['id']);
            $join_game = M('jon_game');
            $join_list = $join_game
                ->join("user on user.u_id=jon_game.u_id")
                ->join("game on game.g_id=jon_game.g_id")
                ->order('j_date desc')
                ->where("jon_game.u_id='$u_id'")
                ->field('jon_game.*,game.*,user.u_name')
                ->select();
            $this->assign('join_list', $join_list);
            $this->display();
        }
    }

    //线下支付
    public function apply($id)
    {
        if (!($_SESSION['manager'])) {
            $this->redirect('d_login', '未登录', 2);
        } else {
            $u_id = (int)($_GET['id']);
            $jon_game = M('jon_game');
            $data['pay_status'] = 1;
            $data['j_note'] = 1;
            $rel = $jon_game->where("u_id ='$u_id'")->save($data);
            if ($rel) {
                $this->success('操作成功');
            }
        }
    }

    //所有人的报名情况
    public function applyshow()
    {
        if (empty($_SESSION['manager'])) {
            $this->redirect('d_login', '未登录', 2);
        } else {
            $user = M('user');
            $player_list = $user->query("select * from user");
            $this->assign('player_list', $player_list);
            $this->display();
        }
    }

    //检录展示
    public function check_m()
    {
        $game = M('game');
        $gamelist = $game->where("g_flag!=2")->limit(5)->order('g_start asc')->select();
        $this->assign('ck_gamelist', $gamelist);
        $time = date('Y-m-d', time());
        $this->assign('nowDate', $time);
        $this->display();
    }

    //所有比赛
    public function game()
    {
        $all_game = M('all_game');
        $gamelist = $all_game->select();
        //分比赛
        $game = M('game');
        $this->assign('all_gamelist', $gamelist);
    }

    //场次的修改
    public function changechang()
    {
        $game = -1;
        $team = -1;
        $this->game();
        if ($_GET['step'] == 'su') {
            $game = 1;//前台赛事
            $all_id = I('get.id');
            $g_list = M('game')->query("select * from game where all_id='$all_id'");
            $this->assign('g_list', $g_list);
        }
        if ($_GET['step'] == 'last') {
            $game = 1;
            $team = 1;
            $g_id = I('get.g_id');
            $ga_list = M('game')->where("g_id='$g_id'")->select();
            $this->assign('ga_list', $ga_list);
        }
        if (IS_POST) {
            $game = M('game');
            $data['g_name'] = I('post.gamename');
            $data['g_start'] = I('post.btime');
            $data['g_info'] = I('post.info');
            $data['min_age'] = I('post.bage');
            $data['max_age'] = I('post.eage');
            $data['g_sex'] = I('post.sex');
            $data['g_type'] = I('post.gameType');
            $data['g_maxperson'] = (int)(I('post.num'));
            $data['g_short'] = I('post.short');
            $data['g_money'] = (int)(I('post.money'));
            $time = strtotime($data['g_start']);
            $data['g_date'] = date("Y-m-d", $time);
            if ($data['g_type'] == '职业组') {
                $data['auth_id'] = 1;
            } elseif ($data['g_type'] == '专业组') {
                $data['auth_id'] = 2;
            } elseif ($data['g_type'] == '业余组') {
                $data['auth_id'] = 3;
            } elseif ($data['g_type'] == '女子六人组') {
                $data['auth_id'] = 4;
            } else {
                $data['auth_id'] = 5;
            }
            $rel = $game->where("g_id='$g_id'")->save($data);
            if ($rel) {
                $this->success('修改成功', U('index/changechang'), 2);
            } else {
                $this->error('部分参数有问题', U('index/changechang'), 2);
            }
        }
        $this->assign('zubie', $team);
        $this->assign('saishi', $game);
        $this->display();
    }

    //大比赛的详情
    public function changeGame($id)
    {
        $g_id = I('GET.id');
        $all_game = M('all_game');
        $all_list = $all_game->where("all_id='$g_id'")->select();
        $this->assign('all_list', $all_list);
        $this->display();
    }

    public function be_check($id)
    {
        $game = M('game');
        //检录
        $id = (int)($_GET['id']);
        //正在检录
        $data['g_flag'] = 1;
        $rel = $game->where("g_id='$id'")->save($data);
        if ($rel) {
            $this->success('成功');
        }
    }

    public function end_check($id)
    {
        $game = M('game');
        //检录
        $id = (int)($_GET['id']);
        //检录结束
        $data['g_flag'] = 2;
        $rel = $game->where("g_id='$id'")->save($data);
        if ($rel) {
            $this->success('检录成功');
        }
    }

    //发布场次
    public function Match_m()
    {
        $game = M('game');
        $stu = -1;
        $this->game();
        if ($_GET['st'] == 'fir') {
            $data['all_id'] = I('get.aid');
            $stu = 1;
        }
        if (IS_POST) {
            $data['g_name'] = I('post.gamename');
            $data['g_info'] = I('post.info');
            $data['min_age'] = I('post.bage');
            $data['man_age'] = I('post.eage');
            $data['g_sex'] = I('post.sex');
            $data['g_maxperson'] = (int)(I('post.num'));
            $data['g_short'] = I('post.short');
            $data['g_money'] = (int)(I('post.money'));
            $data['g_type'] = I('post.gameType');
            $data['g_start'] = I('post.btime');
            $time = strtotime($data['g_start']);
            $data['g_date'] = date("Y-m-d", $time);
            if ($data['g_type'] == '职业组') {
                $data['auth_id'] = 1;
            } elseif ($data['g_type'] == '专业组') {
                $data['auth_id'] = 2;
            } elseif ($data['g_type'] == '业余组') {
                $data['auth_id'] = 3;
            } elseif ($data['g_type'] == '女子六人组') {
                $data['auth_id'] = 4;
            } else {
                $data['auth_id'] = 5;
            }
            $rel = $game->add($data);
            if ($rel) {
                $this->success('发布成功', U('index/Match_m'), 3);
            } else {
                $this->error('场次中某地方有问题', U('index/Match_m'), 1);
            }
        }
        $this->assign('stu', $stu);
        $this->display();
    }

    //添加赛事
    public function add_game()
    {
        $data['all_name'] = I('post.gameName');
        $data['all_start'] = $_POST['gameDate'];
        $data['all_e_time'] = I('post.login_endTime');
        $data['all_b_time'] = I('post.login_sTime');
        $data['all_info'] = $_POST['content'];
        $rel = M('all_game')->add($data);
        if ($rel) {
            $this->success('添加成功');
        } else {
            $this->error('添加赛事中有问题');
        }
    }

    //比赛状态
    public function end_announce()
    {
        $game = M('game');
        $id = int($_GET['id']);
        if (IS_POST) {
            $data['g_status'] = 2;
            $rel = $game->where("m_id='$id'")->save($data);
            if ($rel) {
                $this->success('设置成功');
            }
        }
    }

    //编辑管理员
    public function changeadmin()
    {
        //展示管理员
        $manager = M('manager');
        $manager_list = $manager->select();
        $this->assign('manager_list', $manager_list);
        //添加
        if (IS_POST) {
            $data['mn_user'] = I('post.user');
            $data['mn_password'] = I('post.password');
            $data['auth_id'] = I('post.auth');
            $mn_id = $_POST['id'];
            $rel = $manager->where("mn_id='$mn_id'")->save($data);
            if ($rel) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        }
        $this->display();
    }

    public function delete_manager($id)
    {
        $mn_id = (int)(I('get.id'));
        $manager = M('manager');
        $rel = $manager->where("mn_id ='$mn_id'")->delete();
        if ($rel) {
            $this->success('删除成功');
        }
    }

    //删除场次
    public function deletechang($id)
    {
        $g_id = (int)(I('get.id'));
        $game = M("game");
        $rel = $game->where("g_id='$g_id'")->delete();
        if ($rel) {
            $this->success('删除成功');
        }
    }

    //修改报名信息
    public function changeapply($id)
    {
        $jonMap['j_id'] = I('id');
        $jonGame = M('jon_game')->where($jonMap)->find();

        //查找小比赛
        $gameMap['g_id'] = $jonGame['g_id'];
        $gameEntity = M('game')->where($gameMap)->find();

        //查找大比赛
        $allGameMap['all_id'] = $gameEntity['all_id'];
        $allGame = M('all_game')->where($allGameMap)->find();


        //带头人
        $_topUser = M('user')->where(array('u_id' => $jonGame['u_id']))->find();

        //舞伴ID
        $wdIds = json_decode($jonGame['wb_ids'], true);

        $wdIds[] = 0;
        $wdMap['u_id'] = array('in', $wdIds);
        $wdList = M('user')->where($wdMap)->select();

        $this->assign('_jon_game', $jonGame);
        $this->assign('_game', $gameEntity);
        $this->assign('_wb_list', $wdList);
        $this->assign('_top_user', $_topUser);
        $this->assign('_all_game', $allGame);
        $this->display();
    }

    //成绩公布
    public function grades()
    {
        $game = -1;
        $team = -1;
        $this->game();
        if ($_GET['step'] == 'su') {
            $game = 1;//前台赛事
            $all_id = I('get.id');
            $g_list = M('game')->query("select * from game where all_id='$all_id'");
            $this->assign('g_list', $g_list);
        }
        if ($_GET['step'] == 'last') {
            $g_id = I('get.g_id');
            $game = 1;
            $team = 1;
            redirect(U('project/upGrade', array('g_id' => $g_id)));

            $join_game = M('jon_game');
            $jon_list = $join_game->where("g_id='$g_id'")->select();
            /**
             *
             */
            $this->assign('jon_list', $jon_list);
        }
        if (IS_POST) {
            $u_id = (int)($_POST['uid']);
            $data['end_grade'] = I('post.grade');
            $rel = M('jon_game')->where("g_id='$g_id' && u_id='$u_id'")->save($data);
            if ($rel) {
                $this->success("成功");
            } else {
                $this->error("失败");
            }
        }
        $this->assign('zubie', $team);
        $this->assign('saishi', $game);
        $this->display();
    }

    public function publis()
    {
        if (IS_POST) {
            $data['publish'] = $_POST['content'];
            $data['time'] = date('Y-m-d', time());
            $publish = M('publish');
            $rel = $publish->add($data);
            if ($rel) {
                $this->success("成功");
            }
        }
        $this->display();
    }

    //比赛管理
    public function grades_m()
    {
        if (empty($_SESSION['manager'])) {
            $this->redirect('d_login', '未登录', 5);
        } else {
            $game = M('game');
            $game_list = $game->where("g_flag=2 and g_status!=2")->select();
            $this->assign('game_list', $game_list);
            $this->display();
        }
    }

    //正在比赛
    public function be_game($id)
    {
        $game = M('game');
        $id = (int)($_GET['id']);
        $data['g_status'] = 1;
        $rel = $game->where("g_id='$id'")->save($data);
        if ($rel) {
            $this->success('成功');
        }
    }

    //结束比赛
    public function end_game($id)
    {
        $game = M('game');
        $id = (int)($_GET['id']);
        $data['g_status'] = 2;
        $rel = $game->where("g_id='$id'")->save($data);
        if ($rel) {
            $this->success('结束成功');
        }
    }

    public function match($id)
    {
        $all_id = (int)($_GET['id']);
        //大比赛
        $all_game = M('all_game');
        $gameInfo = $all_game->where("all_id = '$all_id'")->select();
        $this->assign('gameInfo', $gameInfo);
        if (IS_POST) {
            $data['all_name'] = I('post.gname');
            $data['all_info'] = $_POST['content'];
            $data['all_e_time'] = I('post.etime');
            $data['all_start'] = I('post.stime');
            $rel = $all_game->where("all_id='$all_id'")->save($data);
            if ($rel) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        }
        $this->display();
    }

    //登出
    public function loginout()
    {
        session('[destroy]');
        $this->redirect('index/d_login', '退出成功', 0);
    }

    //添加管理员
    public function addadmin()
    {
        if (IS_POST) {
            $data['mn_user'] = I('post.managenum');
            $data['mn_password'] = I('post.managerpwd');
            $data['auth_id'] = I('post.auth');
            $manager = M('manager');
            $result = $manager->add($data);
            if ($result) {
                $this->success('添加成功', 'addadmin', 1);
            } else {
                $this->error('添加失败');
            }
        }
        $this->display();
    }

    //删除赛事
    public function delete_game($id)
    {
        $id = I('get.id');
        $id = (int)($id);
        $game = M('all_game');
        $game->where("all_id = '$id'")->delete();
        $result = M('game')->where("all_id='$id'")->delete();
        if ($result) {
            $this->success('删除成功', U('index/changeMatch'), 1);
        }
    }

    //修改赛事
    public function changeMatch()
    {
        if (empty($_SESSION['manager'])) {
            $this->redirect('d_login', '未登录', 5);
        } else {
            $this->game();
            $this->display();
        }
    }

    //上传
    public function uploa()
    {
        $this->game();
        $up = -1;
        if ($_GET['sud'] == 'sud') {
            $aid = I('get.aid');
            $this->assign('aid', $aid);
            $up = 1;
        }
        $this->assign('up', $up);
        $this->display();
    }

    //上传文件
    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'doc', 'docx', 'xlsx');// 设置附件上传类型
        $upload->rootPath = './Public/upload/bloder/'; // 设置附件上传根目录
        $upload->autoSub = false;
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            $thing = array();
            foreach ($info as $file) {
                static $i = 0;
                $thing["$i"] = $file['savename'];
                $i = $i + 1;
            }
            $idphoto = M('all_game');
            $data['all_g_photo'] = $thing[0];
            $data['all_traffic'] = $thing[1];
            $data['all_guide'] = $thing[2];
            $data['all_place'] = $thing[3];
            $all_id = I('post.allid');
            $idphoto->where("all_id='$all_id'")->save($data);
            $this->success('上传成功');
        }
    }

    //导入
    //导入场次
    public function uploadMatch()
    {
        $upload = new \Think\Upload();
        $upload->exts = array('xlsx', 'xls');
        $upload->rootPath = './Public/upload/';
        $upload->autoSub = false;
        $info = $upload->upload();
        vendor("PHPExcel.PHPExcel");
        foreach ($info as $file) {
            $file_name = './Public/upload/' . $file['savename'];
        }
        $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        for ($i = 2; $i <= $highestRow; $i++)//这个地方根据需要,一般第一行是名称,所以从第二行开始循环,也可以从第一行开始
        {
            $data['g_name'] = $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();//数据库字段和excel列相对应
            $data['g_info'] = $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();
            $data['g_start'] = $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();
            $e_time = ($data['g_start'] - 70 * 365 - 19) * 86400 - 8 * 3600;
            $data['g_start'] = date("Y-m-d H:i:s", $e_time);
            $data['g_short'] = $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue();
            $data['g_maxperson'] = $objPHPExcel->getActiveSheet()->getCell("E" . $i)->getValue();
            $data['min_age'] = $objPHPExcel->getActiveSheet()->getCell("F" . $i)->getValue();
            $data['max_age'] = $objPHPExcel->getActiveSheet()->getCell("G" . $i)->getValue();
            $data['g_sex'] = $objPHPExcel->getActiveSheet()->getCell("H" . $i)->getValue();
            $data['g_type'] = $objPHPExcel->getActiveSheet()->getCell("I" . $i)->getValue();
            $data['g_money'] = $objPHPExcel->getActiveSheet()->getCell("J" . $i)->getValue();
            $data['g_date'] = date("Y-m-d", $e_time);
            if ($data['g_type'] == '职业组') {
                $data['auth_id'] = 1;
            } elseif ($data['g_type'] == '专业组') {
                $data['auth_id'] = 2;
            } elseif ($data['g_type'] == '业余组') {
                $data['auth_id'] = 3;
            } elseif ($data['g_type'] == '女子六人组') {
                $data['auth_id'] = 4;
            } else {
                $data['auth_id'] = 5;
            }
            M('game')->add($data);//插入数据库
        }
        $this->success('导入成功!');
    }
    //导入excel
    /**
     * 导入excel文件   比赛
     */
    public function uploadFile()
    {
        $upload = new \Think\Upload();
        $upload->exts = array('xlsx', 'xls');
        $upload->rootPath = './Public/upload/';
        $upload->autoSub = false;
        $info = $upload->upload();
        vendor("PHPExcel.PHPExcel");
        foreach ($info as $file) {
            $file_name = './Public/upload/' . $file['savename'];
        }
        $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        for ($i = 2; $i <= $highestRow; $i++)//这个地方根据需要,一般第一行是名称,所以从第二行开始循环,也可以从第一行开始
        {
            $data['all_name'] = $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();//数据库字段和excel列相对应
            $data['all_e_time'] = $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();
            $e_time = ($data['all_e_time'] - 70 * 365 - 19) * 86400 - 8 * 3600;
            $data['all_e_time'] = date("Y-m-d H:i:s", $e_time);
            $data['all_start'] = $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue();
            $s_time = ($data['all_start'] - 70 * 365 - 19) * 86400 - 8 * 3600;
            $data['all_start'] = date("Y-m-d H:i:s", $s_time);
            M('all_game')->add($data);//插入数据库
        }
        $this->success('导入成功！');
    }

    //报名表
    public function login_list()
    {
        $join_game = M('jon_game');
        $join_list = $join_game->join("user on user.u_id=jon_game.u_id")->join("jon_user on jon_game.j_id=jon_user.j_id")->join("game on game.g_id=jon_game.g_id")->select();
        $this->export($join_list);
    }


}

?>